package com.sky.financelake.retrofit;

import androidx.lifecycle.MutableLiveData;

import com.sky.financelake.bean.dto.QuotationDto;
import com.sky.financelake.bean.dto.SimpleDayKDto;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class QuotationRepository {

    private static QuotationRepository quotationRepository;

    public static QuotationRepository getInstance() {
        if (quotationRepository == null) {
            quotationRepository = new QuotationRepository();
        }
        return quotationRepository;
    }

    private Api api;

    public QuotationRepository() {
        api = RetrofitService.cteateService(Api.class);
    }

    public void getAllQuotationByCode(MutableLiveData data, String code) {
        api.getAllQuotationByCode(code).enqueue(new Callback<ApiResponse<List<QuotationDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<QuotationDto>>> call, Response<ApiResponse<List<QuotationDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<QuotationDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getThreeRed(MutableLiveData data) {
        api.getThreeRed().enqueue(new Callback<ApiResponse<List<SimpleDayKDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SimpleDayKDto>>> call, Response<ApiResponse<List<SimpleDayKDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SimpleDayKDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getCombinationResult(MutableLiveData data,String combination) {
        api.getCombinationResult(combination).enqueue(new Callback<ApiResponse<List<SimpleDayKDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SimpleDayKDto>>> call, Response<ApiResponse<List<SimpleDayKDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SimpleDayKDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }
}
